<template>
  <div
    id="back"
    :style="{height: (screeHeight-20)+'px', backgroundImage: backgroundImage, backgroundSize: '100% 100%'}"
  >
    <el-card class="up-card up-block">
      <el-form status-icon label-width="0px" class="demo-ruleForm form">
        <el-form-item>
          <h1>用户登录</h1>
        </el-form-item>
        <el-form-item>
          <el-input type="text" v-model="account" placeholder="用户名"></el-input>
        </el-form-item>
        <el-form-item>
          <el-input type="password" v-model="password" placeholder="密码"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="login()" style="width: 100%">登录</el-button>
        </el-form-item>
        <el-form-item>
          <label style="margin-right: 10px;" @click="showRegister()">注册</label>
        </el-form-item>
      </el-form>
    </el-card>

    <el-dialog title="注册" :visible.sync="dialogFormVisible">
      <el-form>
        <el-form-item label="账号">
          <el-input type="text" v-model="regUsername"></el-input>
        </el-form-item>
        <el-form-item label="密码">
          <el-input type="password" v-model="regPassword"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submit">确定</el-button>
        <el-button @click="reset()">取消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {mapMutations} from 'vuex';

export default {
  data() {
    return {
      account: "",
      password: "",
      user: {},
      screenWidth: document.documentElement.clientWidth, // 屏幕宽
      screeHeight: document.documentElement.clientHeight, // 屏幕高
      backgroundImage: "url(" + require("../assets/login.jpeg") + ")",
      loginForm: {
        account: '',
        password: ''
      },
      userToken: '',
      dialogFormVisible: false,
      regUsername: "",
      regPassword: ""
    };
  },

  methods: {
    ...mapMutations(['changeAuthorization']),
    //   点击登录
    login: function () {
      if (this.account === '') {
        this.$message({message: "请输入账号", type: "error"});
      } else if (this.password === '') {
        this.$message({message: "请输入密码", type: "error"})
      }
      this.user["account"] = this.account;
      this.user["password"] = this.password;
      this.$http({
        method: "post",
        url: "/login",
        data: this.user
      }).then(response => {
        if (response.data.code === 0) {
          this.$message({message: response.data.msg, type: "success"});
          this.userToken = response.data.data;
          // 将用户token保存到vuex中
          this.$store.commit('changeAuthorization', {Authorization: this.userToken});
          this.$store.commit('changeUsername', this.account);
          this.$router.push('/index');
        } else {
          this.$message({message: response.data.msg, type: "error"});
        }
      }).catch(err => {
        console.log(err);
      });
    },
    showRegister: function () {
      this.dialogFormVisible = true;
    },
    submit: function () {
      this.$http({
        method: "post",
        url: "/register",
        data: {"account": this.regUsername, "password": this.regPassword}
      }).then(response => {
        if (response.data.code === 0) {
          this.$message({
            message: "注册成功",
            type: "success"
          });
          this.dialogFormVisible = false;
        } else {
          this.$message({
            message: "注册失败，" + response.data.msg,
            type: "error"
          });
        }
      })
    },
    reset() {
      this.dialogFormVisible=false
    }
  }
}
</script>
<style>
#back {
  height: 700px;
}

.up-card {
  display: inline-block;
  width: 500px;
  margin-top: 200px;
}

/*.form {*/
/*  background: white;*/
/*  width: 500px;*/
/*  text-align: center;*/
/*  position: absolute;*/
/*  margin-left: 30%;*/
/*  margin-top: 200px;*/
/*  border-radius: 5px;*/
/*  !*background-color: rgba(255, 255, 255, 0);*!*/
/*}*/

/*.form input {*/
/*  width: 400px;*/
/*  height: 50px;*/
/*  margin-top: 10px;*/
/*  border: 0px;*/
/*}*/
</style>

